﻿
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace homework5
{
    public class Repository<T> where T : class
    {
        private readonly DbContext _context;
        private readonly DbSet<T> _dbSet;

        public Repository(DbContext context)
        {
            _context = context;
            _dbSet = context.Set<T>();
        }

        // 增加单个实体
        public async Task AddAsync(T entity)
        {
            await _dbSet.AddAsync(entity);
            await _context.SaveChangesAsync();
        }

        // 批量增加实体
        public async Task AddRangeAsync(IEnumerable<T> entities)
        {
            await _dbSet.AddRangeAsync(entities);
            await _context.SaveChangesAsync();
        }

        // 查询所有数据
        public async Task<IEnumerable<T>> GetAllAsync()
        {
            return await _dbSet.ToListAsync();
        }

        // 根据条件查询
        public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
        {
            return await _dbSet.Where(predicate).ToListAsync();
        }

        // 查询单个实体，通过主键
        public async Task<T> GetByIdAsync(int id)
        {
            return await _dbSet.FindAsync(id);
        }

        // 更新实体
        public async Task UpdateAsync(T entity)
        {
            _dbSet.Update(entity);
            await _context.SaveChangesAsync();
        }

        // 删除单个实体
        public async Task DeleteAsync(T entity)
        {
            _dbSet.Remove(entity);
            await _context.SaveChangesAsync();
        }

        // 根据ID删除实体
        public async Task DeleteByIdAsync(int id)
        {
            var entity = await _dbSet.FindAsync(id);
            if (entity != null)
            {
                _dbSet.Remove(entity);
                await _context.SaveChangesAsync();
            }
        }
    }
    public class SchoolContext : DbContext
    {
        public DbSet<School> Schools { get; set; }
        public DbSet<Class> Classes { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<Log> Logs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=school.db"); // 使用SQLite数据库
        }
    }
    public class SchoolService
    {
        private readonly Repository<School> _schoolRepository;

        public SchoolService(Repository<School> schoolRepository)
        {
            _schoolRepository = schoolRepository;
        }

        public async Task AddSchoolAsync(School school)
        {
            await _schoolRepository.AddAsync(school);
        }

        public async Task<IEnumerable<School>> GetAllSchoolsAsync()
        {
            return await _schoolRepository.GetAllAsync();
        }

    }

    public class ClassService
    {
        private readonly Repository<Class> _classRepository;

        public ClassService(Repository<Class> classRepository)
        {
            _classRepository = classRepository;
        }

        public async Task AddClassAsync(Class classroom)
        {
            await _classRepository.AddAsync(classroom);
        }

        public async Task<IEnumerable<Class>> GetClassesBySchoolIdAsync(int schoolId)
        {
            return await _classRepository.FindAsync(c => c.SchoolId == schoolId);
        }
    }

    public class StudentService
    {
        private readonly Repository<Student> _studentRepository;

        public StudentService(Repository<Student> studentRepository)
        {
            _studentRepository = studentRepository;
        }

        public async Task AddStudentAsync(Student student)
        {
            await _studentRepository.AddAsync(student);
        }

        public async Task<IEnumerable<Student>> GetStudentsByClassIdAsync(int classId)
        {
            return await _studentRepository.FindAsync(s => s.ClassId == classId);
        }
    }
}
